package guowei.ssoserver.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

@Controller
public class AllLogin {
    private String clientUrl;

//跳转到登陆界面
    @GetMapping("/login")
    public String serverlogin(@RequestParam(value = "url",required = false) String url, Model model,
                              @CookieValue(value = "name",required = false) String name){
        if (!StringUtils.isEmpty(name)){
            System.out.println("cookie的值为:"+name);
            if (StringUtils.isEmpty(url)){
                return "success";
            }else {
                return "redirect://"+url+"?token="+name;
            }
        }
        model.addAttribute("url",url);
        return "login";
    }

//中央机登录都会留下痕迹
    @PostMapping("/info")
    public String logineddd(String age, String name,
                            @RequestParam(value = "url",required = false) String url,
                            HttpServletResponse response){
        System.out.println(age);
        System.out.println(name);
        System.out.println(url);
        Cookie loginUser = new Cookie("name", name);
        response.addCookie(loginUser);
//        String s = UUID.randomUUID().toString().replace("-", "").substring(0,6);
        if (StringUtils.isEmpty(url)){
            return "success";
        }
        return "redirect://"+url+"?token="+name;
    }

}
